package com.jy.adscm.operation.pebase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeDocument;
import com.jy.adscm.operation.pebase.bean.PeDocumentAbstract;
import com.jy.adscm.operation.pebase.mapper.mysql.PeDocumentAbstractMapper;
import com.jy.adscm.operation.pebase.mapper.mysql.PeDocumentMapper;
import com.jy.adscm.operation.pebase.service.PeDocumentAbstractService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-04-18
 */
@Service
public class PeDocumentAbstractServiceImpl extends ServiceImpl<PeDocumentAbstractMapper, PeDocumentAbstract> implements PeDocumentAbstractService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
     PeDocumentAbstractMapper peDocumentAbstractMapper;
     @Autowired
    PeDocumentMapper peDocumentMapper;
    @Override
    public List<PeDocument> selectDocumentByAid(String aid) throws AdscmSystemException {
        //查询原文件的
        List<PeDocument> peDocuments=new ArrayList<>();
        Wrapper<PeDocumentAbstract> peDocumentAbstractWrapper=new EntityWrapper<>();
        peDocumentAbstractWrapper.eq("aid",aid);
        List<PeDocumentAbstract> peDocumentAbstracts = peDocumentAbstractMapper.selectList(peDocumentAbstractWrapper);
        for (PeDocumentAbstract peDocumentAbstract : peDocumentAbstracts) {
            PeDocument peDocument = peDocumentMapper.selectById(peDocumentAbstract.getDid());
            if (AdscmBaseUtils.getInstance().isNotEmpty(peDocument,null)){
                peDocument.setRelationId(peDocumentAbstract.getId());
                peDocuments.add(peDocument);
                if (AdscmBaseUtils.getInstance().isNotEmpty(peDocument.getCreator(),null)){
                    JyUser jyUser = jyUserMapper.selectById(peDocument.getCreator());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                        peDocument.setCreatorName(jyUser.getName());
                    }else {
                        peDocument.setCreatorName(peDocument.getCreator());
                    }
                }
            }
        }
        return peDocuments;
    }
}
